import * as api from './api';
import {PAGE_CODE} from './constants';
import {
    getCurrentUserPagePermissionsSuccess,
    getCurrentUserPagePermissionsFail,
    getCurrentUserPagePermissionsApi
} from 'Shared/actions/currentUserContext';
export const ERROR_CODE = 'error';
import {conventTableCondition} from '../utils';
import {message} from 'antd';
import {formatMessage} from './intl';

export const GET_INIT_DATA_BEGIN = 'GET_INIT_DATA_BEGIN';
export const GET_INIT_DATA_SUCCESS = 'GET_INIT_DATA_SUCCESS';
export const GET_INIT_DATA_FAIL = 'GET_INIT_DATA_FAIL';
const getInitDataBegin = () => ({
    type: GET_INIT_DATA_BEGIN
});

const getInitDataSuccess = data => ({
    type: GET_INIT_DATA_SUCCESS,
    data
});
const getInitDataFail = () => ({
    type: GET_INIT_DATA_FAIL
});
/**初始化 */
/*==============================查询列表================================= */
export const GET_LIST_DATA_BEGIN = 'GET_LIST_DATA_BEGIN';
export const GET_LIST_DATA_SUCCESS = 'GET_LIST_DATA_SUCCESS';
export const GET_LIST_DATA_FAIL = 'GET_LIST_DATA_FAIL';
export const GET_LIST_TYPE_BEGIN = 'GET_LIST_TYPE_BEGIN';
export const GET_LIST_TYPE_SUCCESS = 'GET_LIST_TYPE_SUCCESS';
export const GET_LIST_TYPE_FAIL = 'GET_LIST_TYPE_FAIL';
const getListDataBegin = () => ({
    type: GET_LIST_DATA_BEGIN
});
const getListTypeBegin = () => ({
    type: GET_LIST_TYPE_BEGIN
});
const getListDataSuccess = data => ({
    type: GET_LIST_DATA_SUCCESS,
    data
});
const getListTypeSuccess = data => ({
    type: GET_LIST_TYPE_SUCCESS,
    data
});
export const getListDataFail = () => ({
    type: GET_LIST_DATA_FAIL
});
export const getListTypeDataFail = () => ({
    type: GET_LIST_TYPE_FAIL
});
export const GET_LIST_COUNTRY_DATA_BEGIN = 'GET_LIST_COUNTRY_DATA_BEGIN';
export const GET_LIST_COUNTRY_DATA_SUCCESS = 'GET_LIST_COUNTRY_DATA_SUCCESS';
export const GET_LIST_COUNTRY_DATA_FAIL = 'GET_LIST_COUNTRY_DATA_FAIL';

const getListCountryDataBegin = () => ({
    type: GET_LIST_COUNTRY_DATA_BEGIN
});
const getListCountryDataSuccess = data => ({
    type: GET_LIST_COUNTRY_DATA_SUCCESS,
    data
});
export const getListCountryDataFail = () => ({
    type: GET_LIST_COUNTRY_DATA_FAIL
});


/*==============================初始化API================================= */
//获取初始化数据和权限
export const getInitData = () => dispatch => {
    dispatch(getInitDataBegin());
    const init = api.getInit().then(res => {
        if(res.ok)
            dispatch(getInitDataSuccess(res.data));
        else
            dispatch(getInitDataFail());
        return res.ok;
    });
    const permissions = getCurrentUserPagePermissionsApi(PAGE_CODE).then(res => {
        if(res.isOk) dispatch(getCurrentUserPagePermissionsSuccess(res.data));
        else dispatch(getCurrentUserPagePermissionsFail(res.statusCode, res.message));
        return res.isOk;
    });

    return Promise.all([init, permissions]).then(values => values[0] && values[1]);
};

/*==============================查询action================================= */
//获取查询列表
export const getList = condition => dispatch => {
    const {type} = condition;
    if(!type)
        return message.warn(formatMessage({
            id: 'action.query.message1',
            defaultMessage: '查询图表类型不能为空'
        }));
    dispatch(getListDataBegin());
    dispatch(getListTypeBegin());
    const queryCondition = (conventTableCondition(condition));
    const list = api.getList(queryCondition).then(res => {
        if(res.ok)
            dispatch(getListDataSuccess(res.data));
    });
    const listType = api.getListType(queryCondition).then(res => {
        if(res.ok)
            dispatch(getListTypeSuccess(res.data));
    });
    return Promise.all([list, listType]).then(values => values[0] && values[1]);
};

/*==============================查询 国家action================================= */
//获取查询列表
export const getListCountry = condition => dispatch => {
    const {type} = condition;
    if(!type)
        return message.warn(formatMessage({
            id: 'action.query.message1',
            defaultMessage: '查询图表类型不能为空'
        }));
    dispatch(getListCountryDataBegin());
    const queryCondition = conventTableCondition(condition);
    if(condition.dealerId.length > 0)
        return api.getListCountry(queryCondition).then(res => {
            if(res.ok)
                dispatch(getListCountryDataSuccess(res.data));
        });
    dispatch(getListCountryDataFail());
};


//修改查询条件
export const SAVE_QUERY_CONDITION = 'SAVE_QUERY_CONDITION';
export const saveQueryCondition = (name, value) => ({
    type: SAVE_QUERY_CONDITION,
    name,
    value
});
//重置查询条件
export const RESET_QUERY_PANEL = 'RESET_QUERY_PANEL';
export const resetQueryPanel = () => ({
    type: RESET_QUERY_PANEL,
});

export const RESET_QUERY_PANEL_DEALER = 'RESET_QUERY_PANEL_DEALER';
export const resetDealerQueryPanel = () => ({
    type: RESET_QUERY_PANEL_DEALER,
});

//查询月按钮搜索
export const querySearch = () => (dispatch, getState) => {
    const condition = getState().getIn(['page', 'appState', 'queryCondition']).toJS();
    dispatch(getList(condition));
};

//查询国家按钮搜索
export const querySearchCountry = () => (dispatch, getState) => {
    const condition = getState().getIn(['page', 'appState', 'queryCondition']).toJS();
    dispatch(getListCountry(condition));
};

